package com.example.mytayunapi.Controller;

import com.example.mytayunapi.Entity.Share_postsInfo;
import com.example.mytayunapi.Entity.userInfo;
import com.example.mytayunapi.Service.share_postsInfoService;
import com.example.mytayunapi.Vo.ShareUserInfo;
import com.example.mytayunapi.Vo.ShareUserProducts;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("share")
public class Share_postsController {
    @Autowired
    private share_postsInfoService share_postsService;

    //http://localhost:8080/share/sharecate
    @GetMapping("/sharecate")
    public List<ShareUserInfo> getShare_postsInfo(String Share_catagory){
        return share_postsService.getSharePosts(Share_catagory);
    }

    //http://localhost:8080/share/sharedetail
    @GetMapping("/sharedetail")
    public List<ShareUserInfo> getSharePostsDetail( Integer Share_id){
        return share_postsService.getSharePostsDetail(Share_id);
    }

    @GetMapping("/sharepeople")
    public List<ShareUserInfo> getSharePostsId(String School_degree){
        return share_postsService.getSharePostsInfoByPeople(School_degree);
    }

    @PostMapping("/delete")
    public Integer deleteSharePosts(Integer Share_id){
        return share_postsService.deleteSharePosts(Share_id);
    }

    //http://localhost:8080/share/posts
    @GetMapping("/posts")
    public List<ShareUserInfo> queryPosts(){
        return share_postsService.getSharePostsInfo();
    }

    //http://localhost:8080/share/queryshare
    @GetMapping("/queryshare")
    public List<ShareUserInfo> queryShare(String Share_title){
        List<ShareUserInfo> Share = null;
        if (Share_title==null || Share_title.equals("")){
            Share =  share_postsService.getSharePostsInfo();
        }else {
            Share = share_postsService.getShareTitleInfo(Share_title);
        }
        return Share;
    }

    //http://localhost:8080/share/upshare
    @PostMapping("/upshare")
    public Integer updateShareStatus(@RequestBody ShareUserInfo shareUserInfo){
        return share_postsService.updateShareStatus(shareUserInfo);
    }

    //分页
    // http://localhost:8080/share/page
    @GetMapping("/page")
    public PageInfo<ShareUserInfo> pageInfo(@RequestParam Integer page, @RequestParam Integer size){
        return share_postsService.getPageInfo(page,size);
    }
//
    //http://localhost:8080/share/queryShareSta
    @GetMapping("/queryShareSta")
    public List<ShareUserInfo> queryShareSta( String Share_status){
        return share_postsService.getSharePostsInfoStatus(Share_status);
    }

    //http://localhost:8080/share/queryShareStaNa
    @GetMapping("/queryShareStaNa")
    public List<ShareUserInfo> queryShareStaNa(String Share_status){
        return share_postsService.getSharePostsInfoStatusNa(Share_status);
    }

    //http://localhost:8080/share/querytiAcate
    @GetMapping("/querytiAcate")
    public List<ShareUserInfo> querytiAcate(@RequestParam(value = "keyword",required = false) String keyword,
                                            @RequestParam(value = "Share_status",required = false) String Share_status){
        if(keyword!=null&&Share_status!=null) {
            return share_postsService.getSharetitleAndWord(keyword, Share_status);
        }else if(Share_status!=null){
            return share_postsService.getSharePostsInfoStatusNa(Share_status);
        }else if(keyword!=null){
            return share_postsService.getShareTitle(keyword);
        }else{
            return Collections.emptyList();
        }
    }

    //http://localhost:8080/share/querytitle
    @GetMapping("/querytitle")
    public List<ShareUserInfo> querytitle(@RequestParam(value = "keyword",required = false) String keyword){
        return share_postsService.getShareTitle(keyword);
    }

    //http://localhost:8080/share/querycate
    @GetMapping("/querycate")
    public List<ShareUserInfo> querycate(@RequestParam(value = "word",required = false) String word){
        return share_postsService.getShareTitleAndCate(word);
    }

    //http://localhost:8080/share/querytitleAcate
    @GetMapping("/querytitleAcate")
    public List<ShareUserInfo> querytitleAcate(@RequestParam(value = "keyword",required = false) String keyword,
                                               @RequestParam(value = "word",required = false) String word){
        if(keyword != null&&word != null) {
            return share_postsService.getShareTitleAndCateStatus(keyword,word);
        }else if(word!=null){
            return share_postsService.getShareTitleAndCate(word);
        }else if(keyword!=null){
            return share_postsService.getShareTitle(keyword);
        }else{
            return Collections.emptyList();
        }

    }

    //http://localhost:8080/share/maijia
    @GetMapping("/maijia")
    public List<ShareUserProducts> maijia(@RequestParam Integer Product_id){
        return share_postsService.getShareUserProducts(Product_id);
    }
}
